n = []
for j in range (2,10000):
    a = 1
    for l in range(2, j//2):
        if j % l == 0:
            a = 0
            break
    if a:
        n.append(j)
m = 0
s = input()
if not (999 < int(s) < 10000):
    s = '0000'
for i0 in '' , s[0]:
    for i1 in '' , s[1]:
        for i2 in '' , s[2]:
            for i3 in '' , s[3]:
                if i0+i1+i2+i3:
                    f = int(i0+i1+i2+i3)
                    if f in n:
                        m = max(m, f)
print(m)
